package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import org.come.entity.Goodstable;
import org.come.mouslisten.PawnChoseMouslisten;
import org.come.mouslisten.SurePawnMouslisten;
import org.come.until.EquipTool;
import org.come.until.GoodsListFromServerUntil;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.RoleOperationPanelBtn;
import com.tool.btn.goodbtn;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;



//TODO 当铺、典当
/**
 * 类说明
 *
 * @author 黄建彬
 * @date 2017年12月21日 新建
 *
 *       典当面板
 */
public class PawnJfpanel extends JPanel {
    private RoleOperationPanelBtn SurePawn;// 确认典当按钮监听
    private JTextArea goodsCount, goodsSum;// 数量和金额总额
    private JLabel[] goodsJlabel = new JLabel[24];// 用来遍历典当里面的东西
    private int goodPosition;// 判断预选框的位置

    // 放置背包切换按钮
    private goodbtn[] btnrights;

    public PawnJfpanel() throws Exception {
        if (MyIsif.getStyle().equals("水墨UI")){
            this.setBackground(UIUtils.Color_BACK);
            this.setPreferredSize(new Dimension(418, 379));
            this.setLayout(null);
            this.setOpaque(false);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 29);
            offBtn.setBounds(418 - 37, 10, 25, 25);
            this.add(offBtn);

            SurePawn = new RoleOperationPanelBtn("skin/8100226/newbutton/B22.png", 1, "典 当", null,null);
            SurePawn.setBounds(143, 325, 99, 24);
            this.add(SurePawn);
            // 数量框
            goodsCount = new JTextArea("0");
            goodsCount.setOpaque(false);
            goodsCount.setBounds(143, 264, 140, 18);
            goodsCount.setForeground(Color.yellow); // 设置字体颜色
            // 限制输入内容为数字切数字不大于100
            goodsCount.addKeyListener(new KeyListener() {
                @Override
                public void keyTyped(KeyEvent e) {
                    // TODO Auto-generated method stub
                    int charstr = e.getKeyChar();
                    String str = goodsCount.getText();
                    if (str.length() == 0) {
                        goodsCount.setText(0 + "");
                    }
                    // control the value between 0 and 9
                    if (charstr < KeyEvent.VK_0 || charstr > KeyEvent.VK_9) {
                        e.consume();
                    }

                }

                @Override
                public void keyPressed(KeyEvent e) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void keyReleased(KeyEvent e) {
                    // TODO Auto-generated method stub

                }

            });
            this.add(goodsCount);
            // 金额
            goodsSum = new JTextArea("0");
            goodsSum.setBounds(143, 289, 140, 18);
            goodsSum.setOpaque(false);
            goodsSum.setForeground(Color.yellow); // 设置字体颜色
            this.add(goodsSum);
            int a = 0;// 用来区分列
            int b = -1;// 区分行坐标
            for (int i = 0; i < 24; i++) {
                if ((i > 0) && (i % 6 == 0)) {
                    a++;
                    b = -1;
                }
                b++;
                goodsJlabel[i] = new JLabel();// 设置jlabel
                goodsJlabel[i].setBounds(49 + b * 51, 33 + a * 51, 50, 50);
                goodsJlabel[i].addMouseListener(new PawnChoseMouslisten(i, this));
                this.add(goodsJlabel[i]);
            }
            btnrights = new goodbtn[6];
            for(int i = 0; i < this.btnrights.length; ++i) {
                this.btnrights[i] = new goodbtn("skin/8100226/newbutton/C0"+(i+1)+".png", 0, this, i);
                this.btnrights[i].setBounds(356, 33 + i * 35, 35, 31);
                this.add(this.btnrights[i]);
            }
        }else {
            this.setBackground(UIUtils.Color_BACK);
            this.setPreferredSize(new Dimension(418, 379));
            this.setLayout(null);
            this.setOpaque(false);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("img/xy2uiimg/8_jpg.button.btn_close.jpg", 1, 29);
            offBtn.setBounds(418 - 37, 10, 25, 25);
            this.add(offBtn);

            SurePawn = new RoleOperationPanelBtn("inkImg/hongmu/1_png.button.btn_xy.png", 1, "典 当", null,null);
            SurePawn.setBounds(143, 325, 99, 24);
            this.add(SurePawn);
            // 数量框
            goodsCount = new JTextArea("0");
            goodsCount.setOpaque(false);
            goodsCount.setBounds(143, 264, 140, 18);
            goodsCount.setForeground(Color.yellow); // 设置字体颜色
            // 限制输入内容为数字切数字不大于100
            goodsCount.addKeyListener(new KeyListener() {
                @Override
                public void keyTyped(KeyEvent e) {
                    // TODO Auto-generated method stub
                    int charstr = e.getKeyChar();
                    String str = goodsCount.getText();
                    if (str.length() == 0) {
                        goodsCount.setText(0 + "");
                    }
                    // control the value between 0 and 9
                    if (charstr < KeyEvent.VK_0 || charstr > KeyEvent.VK_9) {
                        e.consume();
                    }

                }

                @Override
                public void keyPressed(KeyEvent e) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void keyReleased(KeyEvent e) {
                    // TODO Auto-generated method stub

                }

            });
            this.add(goodsCount);
            // 金额
            goodsSum = new JTextArea("0");
            goodsSum.setBounds(143, 289, 140, 18);
            goodsSum.setOpaque(false);
            goodsSum.setForeground(Color.yellow); // 设置字体颜色
            this.add(goodsSum);
            int a = 0;// 用来区分列
            int b = -1;// 区分行坐标
            for (int i = 0; i < 24; i++) {
                if ((i > 0) && (i % 6 == 0)) {
                    a++;
                    b = -1;
                }
                b++;
                goodsJlabel[i] = new JLabel();// 设置jlabel
                goodsJlabel[i].setBounds(49 + b * 51, 33 + a * 51, 50, 50);
                goodsJlabel[i].addMouseListener(new PawnChoseMouslisten(i, this));
                this.add(goodsJlabel[i]);
            }
            btnrights = new goodbtn[6];
            for(int i = 0; i < this.btnrights.length; ++i) {
                this.btnrights[i] = new goodbtn("img/xy2uiimg/44_png.button.tab_pak.png", 0, this, i);
                this.btnrights[i].setBounds(356, 33 + i * 35, 35, 31);
                this.add(this.btnrights[i]);
            }
        }
    }

    private int xz = -1;// 当前要购买的物品位置

    /**
     * 典当结束清除
     */
    public void qingchu() {
        xuanzhong(null, -1,false);
    }

    /**
     * 画出预选边框
     *
     * @return
     */
    public void PaintingText(int shopPlace) {
        goodsJlabel[shopPlace].setBorder(BorderFactory.createLineBorder(Color.red));
    }

    /**
     * 清除预选边框
     *
     * @return
     */
    public void ClearText(int shopPlace) {
        if (xz != shopPlace) {
            goodsJlabel[shopPlace].setBorder(null);
        }
    }

    /**
     * 选中
     */
    public void xuanzhong(Goodstable goodstable, int shopPlace,boolean isAll) {
        if (goodstable == null) {
            if (xz != -1) {
                goodsJlabel[xz].setBorder(null);
            }
            shopPlace = xz;
            xz = -1;
            ClearText(shopPlace);
            goodsCount.setText("0");
            SurePawnMouslisten.setGoodstable(null);
        } else if (!EquipTool.isEquip(goodstable.getType()) && SurePawnMouslisten.getGoodstable() != null
                && goodstable.getGoodsid().intValue() == SurePawnMouslisten.getGoodstable().getGoodsid().intValue()) {
            if (isAll) {
                goodsCount.setText(goodstable.getUsetime() + "");
            } else {
                // 如果这商品之前被点击则购买数量加1
                int sum = Integer.parseInt(goodsCount.getText());
                if (goodstable.getUsetime() > sum) {
                    sum += 1;
                } else {
                    sum = goodstable.getUsetime();
                }
                goodsCount.setText(sum + "");
            }
        } else {
            if (xz != -1) {
                goodsJlabel[xz].setBorder(null);
            }
            xz = shopPlace;
            goodsJlabel[xz].setBorder(BorderFactory.createLineBorder(Color.red));
            SurePawnMouslisten.setGoodstable(goodstable);
            goodsCount.setText("1");// 数量
        }
        if (xz != -1) {
            goodsJlabel[xz].setBorder(BorderFactory.createLineBorder(Color.YELLOW));
        }
    }

    private ImageIcon icon;
    private ImageIcon icon1;

    @Override
    protected void paintComponent(Graphics g) {
        if (MyIsif.getStyle().equals("水墨UI")) {
            super.paintComponent(g);
            // 绘制面板
            if (icon == null) {
                icon = new ImageIcon("inkImg/newbackground/B307.png");
            }

            g.drawImage(icon.getImage(), 0, 0, 418, 379, this);
            GoodsListFromServerUntil.draw(g, 49, 33);
        }else{
            super.paintComponent(g);
            // 绘制面板
            if (icon1 == null) {
                icon1 = new ImageIcon("skin/8100226/button/hongmu/典当红木.png");
            }

            g.drawImage(icon1.getImage(), 0, 0, 418, 379, this);
            GoodsListFromServerUntil.draw(g, 49, 33);
        }
    }

    public RoleOperationPanelBtn getSurePawn() {
        return SurePawn;
    }

    public void setSurePawn(RoleOperationPanelBtn surePawn) {
        SurePawn = surePawn;
    }

    public JTextArea getGoodsCount() {
        return goodsCount;
    }

    public void setGoodsCount(JTextArea goodsCount) {
        this.goodsCount = goodsCount;
    }

    public JTextArea getGoodsSum() {
        return goodsSum;
    }

    public void setGoodsSum(JTextArea goodsSum) {
        this.goodsSum = goodsSum;
    }

    public JLabel[] getGoodsJlabel() {
        return goodsJlabel;
    }

    public void setGoodsJlabel(JLabel[] goodsJlabel) {
        this.goodsJlabel = goodsJlabel;
    }

    public int getGoodPosition() {
        return goodPosition;
    }

    public void setGoodPosition(int goodPosition) {
        this.goodPosition = goodPosition;
    }

    public goodbtn[] getBtnrights() {
        return btnrights;
    }

    public void setBtnrights(goodbtn[] btnrights) {
        this.btnrights = btnrights;
    }



}
